package com.lagou.gongkaike;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

public class GKafkaDemo {
    static int count;
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(10);

        String topic = "animalone";
        SimpleStringSchema simpleStringSchema = new SimpleStringSchema();
        Properties props = new Properties();
        props.setProperty("bootstrap.servers","hdp-2:9092");
        props.setProperty("group.id","mygp");

        FlinkKafkaConsumer<String> consumer = new FlinkKafkaConsumer<>(topic, simpleStringSchema, props);
        consumer.setStartFromEarliest();
        consumer.setCommitOffsetsOnCheckpoints(true);

        DataStreamSource<String> data = env.addSource(consumer);

        SingleOutputStreamOperator<String> mapped = data.rebalance().map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                count++;
                if (count == 7000) {
                    count = 0;
                    throw new Exception("error.");
                }
                return value;
            }
        });

        mapped.print();
        env.execute();
    }
}
